package com.lagou.orders.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.domain.Goods;
import com.lagou.domain.Orders;
import com.lagou.orders.feign.GoodsFeign;
import com.lagou.orders.mapper.OrdersMapper;
import com.lagou.orders.service.OrdersService;
import com.lagou.vo.OrdersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private GoodsFeign goodsFeign;

    @Override
    public Orders getOrdersById(Integer id) {
        //根据id获取订单信息
        Orders orders = ordersMapper.getOrdersById(id);
        List<Integer> goodsIds = ordersMapper.getGoodsIdsByOrderId(id);
        //获取商品信息
        List<Goods> goodsList = goodsFeign.getGoodsById(goodsIds);
        orders.setGoodsList(goodsList);
        return orders;
    }

    @Override
    public Map<String, Object> getOrdersByPage(OrdersVO ordersVO) {
        PageHelper.startPage(ordersVO.getStartPage(),ordersVO.getPageSize());
        List<Orders> ordersList = ordersMapper.getOrdersByPage(ordersVO);
        PageInfo<Orders> info = new PageInfo<>(ordersList);
        Map<String,Object> map = new HashMap<>();
        map.put("list",ordersList);
        map.put("total",info.getTotal());
        return map;

    }
}
